package org.jeecg.modules.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import cn.hutool.core.util.ObjectUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.entity.CuUserDetails;
import org.jeecg.modules.entity.CuUserPlatform;
import org.jeecg.modules.service.ICuUserDetailsService;
import org.jeecg.modules.service.ICuUserPlatformService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

/**
 * @Description: 用户平台
 * @Author: jeecg-boot
 * @Date: 2024-08-26
 * @Version: V1.0
 */
@Tag(name = "用户平台")
@RestController
@RequestMapping("/cuckoo/cuUserPlatform")
@Slf4j
public class CuUserPlatformController extends JeecgController<CuUserPlatform, ICuUserPlatformService> {

    @Autowired
    private ICuUserPlatformService cuUserPlatformService;

    @Autowired
    private ICuUserDetailsService userService;

    /**
     * 分页列表查询
     *
     * @param cuUserPlatform
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "用户平台-分页列表查询")
    @Operation(summary = "用户平台-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<CuUserPlatform>> queryPageList(CuUserPlatform cuUserPlatform,
                                                       @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                       @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                       HttpServletRequest req) {
        QueryWrapper<CuUserPlatform> queryWrapper = QueryGenerator.initQueryWrapper(cuUserPlatform, req.getParameterMap());
        Page<CuUserPlatform> page = new Page<CuUserPlatform>(pageNo, pageSize);
        IPage<CuUserPlatform> pageList = cuUserPlatformService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param cuUserPlatform
     * @return
     */
    @AutoLog(value = "用户平台-添加")
    @Operation(summary = "用户平台-添加")
    @RequiresPermissions("cuckoo:cu_user_platform:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody CuUserPlatform cuUserPlatform) {
        cuUserPlatformService.save(cuUserPlatform);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param cuUserPlatform
     * @return
     */
    @AutoLog(value = "用户平台-编辑")
    @Operation(summary = "用户平台-编辑")
    @RequiresPermissions("cuckoo:cu_user_platform:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody CuUserPlatform cuUserPlatform) {
        cuUserPlatformService.updateById(cuUserPlatform);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "用户平台-通过id删除")
    @Operation(summary = "用户平台-通过id删除")
    @RequiresPermissions("cuckoo:cu_user_platform:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        cuUserPlatformService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "用户平台-批量删除")
    @Operation(summary = "用户平台-批量删除")
    @RequiresPermissions("cuckoo:cu_user_platform:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.cuUserPlatformService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "用户平台-通过id查询")
    @Operation(summary = "用户平台-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<CuUserPlatform> queryById(@RequestParam(name = "id", required = true) String id) {
        CuUserPlatform cuUserPlatform = cuUserPlatformService.getById(id);
        if (cuUserPlatform == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(cuUserPlatform);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param cuUserPlatform
     */
    @RequiresPermissions("cuckoo:cu_user_platform:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, CuUserPlatform cuUserPlatform) {
        return super.exportXls(request, cuUserPlatform, CuUserPlatform.class, "用户平台");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequiresPermissions("cuckoo:cu_user_platform:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, CuUserPlatform.class);
    }


    /**
     * 获取我的平台列表
     */
    @Operation(summary = "用户平台-获取我的平台列表")
    @GetMapping(value = "/platlist")
    public Result<List<CuUserPlatform>> platlist(HttpServletRequest request) {
        String username = JwtUtil.getUserNameByToken(request);
        CuUserDetails user = userService.getByUsername(username);

        CuUserPlatform cuUserPlatform = new CuUserPlatform();
        cuUserPlatform.setUserId(user.getId());
        QueryWrapper<CuUserPlatform> queryWrapper = new QueryWrapper<>(cuUserPlatform);
        List<CuUserPlatform> list = cuUserPlatformService.list(queryWrapper);

        return Result.OK(list);
    }

    /**
     * 添加
     *
     * @param cuUserPlatform
     * @return
     */
    @AutoLog(value = "用户平台-添加")
    @Operation(summary = "用户平台-添加")
    @RequiresPermissions("cuckoo:cu_user_platform:platadd")
    @PostMapping(value = "/platadd")
    public Result<String> platadd(@RequestBody CuUserPlatform cuUserPlatform, HttpServletRequest request) {
        String username = JwtUtil.getUserNameByToken(request);
        CuUserDetails user = userService.getByUsername(username);

        // 重新是否存在
        CuUserPlatform platform = new CuUserPlatform();
        platform.setUserId(user.getId());
        platform.setPlatformId(cuUserPlatform.getPlatformId());
        platform = cuUserPlatformService.getOne(new QueryWrapper<>(platform));
        if (ObjectUtil.isNotEmpty(platform)) {
            return Result.error("平台已存在");
        }

        cuUserPlatform.setUserId(user.getId());
        cuUserPlatformService.save(cuUserPlatform);
        return Result.OK("添加成功");
    }

    /**
     * 编辑
     *
     * @param cuUserPlatform
     * @return
     */
    @AutoLog(value = "用户平台-编辑")
    @Operation(summary = "用户平台-编辑")
    @RequiresPermissions("cuckoo:cu_user_platform:platedit")
    @RequestMapping(value = "/platedit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> platedit(@RequestBody CuUserPlatform cuUserPlatform, HttpServletRequest request) {
        String username = JwtUtil.getUserNameByToken(request);
        CuUserDetails user = userService.getByUsername(username);

        // 先查询 再更新
        CuUserPlatform platform = new CuUserPlatform();
        platform.setUserId(user.getId());
        platform.setPlatformId(cuUserPlatform.getPlatformId());
        platform = cuUserPlatformService.getOne(new QueryWrapper<>(platform));
        if(ObjectUtil.isEmpty(platform)){
            return Result.error("未找到对应数据");
        }

        cuUserPlatform.setId(platform.getId());
        cuUserPlatformService.updateById(cuUserPlatform);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
      * @param platform
     * @param request
     * @return
     */
    @AutoLog(value = "用户平台-通过id删除")
    @Operation(summary = "用户平台-通过id删除")
    @RequiresPermissions("cuckoo:cu_user_platform:platedelete")
    @PostMapping(value = "/platedelete")
    public Result<String> platedelete(@RequestBody CuUserPlatform platform, HttpServletRequest request) {
        String username = JwtUtil.getUserNameByToken(request);
        CuUserDetails user = userService.getByUsername(username);

        // 根据条件查询 用户ID  平台ID
        platform.setUserId(user.getId());
        platform.setPlatformId(platform.getPlatformId());

        QueryWrapper<CuUserPlatform> queryWrapper = new QueryWrapper<>(platform);
        CuUserPlatform cuUserPlatform = cuUserPlatformService.getOne(queryWrapper);

        if (ObjectUtil.isEmpty(cuUserPlatform) || !cuUserPlatform.getUserId().equals(user.getId())) {
            return Result.error("未找到您的数据");
        }

        cuUserPlatformService.removeById(cuUserPlatform.getId());
        return Result.OK("删除成功!");
    }

}
